package controler;

import dao.UsuarioDAO;
import model.Usuario;
import view.GUI_BuscUsuario;
import view.GUI_FormUsuario;
import view.GUI_desktop;

public class Controler_Usuario {
    
    GUI_desktop pai;
    GUI_FormUsuario gui;
    GUI_BuscUsuario bus;
    UsuarioDAO dao;
    Usuario u;
    
    public void initForm(GUI_desktop pai){
        this.pai = pai;
        gui = new GUI_FormUsuario(this.pai, true);
        gui.control = this;
        gui.setVisible(true);
    }
    
    public void initBusc(){
        bus = new GUI_BuscUsuario(pai, true);
        bus.control = this;
        bus.setVisible(true);
    }
    
    public void newForm(){
        gui.btnSalvar.setEnabled(true);
        gui.txtNome.setEditable(true);
        gui.txtCPF.setEditable(true);
        gui.txtSenha.setEditable(true);
        gui.txtRsenha.setEditable(true);
        gui.txtUsuario.setEditable(true);
        gui.txtDesconto.setEditable(true);
        
        gui.txtID.setText(new UsuarioDAO().getLastID());
        gui.txtCPF.setText("");
        gui.txtNome.setText("");
        gui.txtRsenha.setText("");
        gui.txtSenha.setText("");
        gui.txtUsuario.setText("");
        gui.txtDesconto.setText("");
        gui.chkAtivo.setSelected(true);
    }
    
    public void saveForm(){
        u = new Usuario();
        u.setNome(gui.txtNome.getText());
        u.setCPF(gui.txtCPF.getText());
        u.setAtivo(gui.chkAtivo.isSelected());
        u.setUser(gui.txtUsuario.getText());
        u.setSenha(gui.txtSenha.getText());
        u.setDesconto(Double.parseDouble(gui.txtDesconto.getText()));
        u.setDtCadastro(util.Relogio.getThisDate());
        new UsuarioDAO().persist(u);
    }
    
}//CLASS
